package com.hotel.controller;


import com.hotel.pojo.Bill;
import com.hotel.pojo.OrderInfoManager;
import com.hotel.pojo.RoomManager;
import com.hotel.service.BillService;
import com.hotel.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Order")
public class OrderController {
    @Autowired
//    @Qualifier("OrderServiceImpl")---khz
    private OrderService orderService;

    @Autowired
    private BillService billService;

    //显示入住客户信息
    @RequestMapping("/selectOrder")
    public String userList(Model model) {
        List<OrderInfoManager> list = orderService.selectOrder();
        model.addAttribute("list", list);
        //System.out.println(list);
        return "allOrder";
    }

    //显示房间信息
    @RequestMapping("/selectRoomManager")
    public String roomList(Model model){
        List<RoomManager> list = orderService.selectRoomManager();
        model.addAttribute("list",list);
        return "selectRoomManager";
    }

    //跳转到添加入住信息
    @RequestMapping("/toInsertOrder")
    public String toInsertOrder(){
        return "addOrder";
    }

    //添加入住信息
    @RequestMapping("/insertOrder")
    public String insertOrder(OrderInfoManager orderInfoManager) {
        orderService.insertOrder(orderInfoManager);
//----------khz修改
        //在账单中添加入住收益
        billService.checkIn(new Bill(billService.getRoomPrice(orderInfoManager.getRoomId()), orderInfoManager.getCheckInDate(), "房费", "收入"));
        orderService.updateRoomState(orderInfoManager.getRoomId(), 1);
        return "redirect:/Order/selectOrder";
    }


    //跳转updateRoomState
    @RequestMapping("/toUpdateRoomState")
    public String toUpdateRoomState(int roomId,Model model){
        RoomManager roomManager= orderService.querRoomById(roomId);
        model.addAttribute("UpdateRoom",roomManager);
        return "updateRoomState";
    }
    //更新客房状态
    @RequestMapping("/updateRoomState")
    public String updateRoomState(Integer roomId,Integer roomState){
        orderService.updateRoomState(roomId,roomState);
        //System.out.println(roomId);
        //System.out.println(orderService.updateRoomState(roomId,roomState));
        return "redirect:/Order/selectOrder";
    }

    //跳转到insertOutOrder
    @RequestMapping("/toInsertOutOrder")
    public String toInsertOutOrder(String userPhone, Model model) {
        OrderInfoManager orderInfoManager = orderService.queryUser(userPhone);
        //System.out.println(userPhone);
        //System.out.println(orderInfoManager);
        model.addAttribute("OutDate", orderInfoManager);
        return "toInsertOutOrder";
    }

    //更新客户离开信息(退房)
    @RequestMapping("/insertOutOrder")
//----------khz修改
//    初始形参String checkOutDate,String userPhone
    public String insertOutOrder(OrderInfoManager orderInfoManager) {
        orderService.insertOutHotel(orderInfoManager);
        return "redirect:/Order/selectOrder";
    }

    //删除客户
    @GetMapping("/deleteUserInfo")
    public String deleteUserInfo(Long userPhone) {
        //System.out.println(userPhone);
        orderService.deleteUserInfo(userPhone);
        return "redirect:/Order/selectOrder";
    }

    //查询客户
    @RequestMapping("/referUserName")
    public String queryUserName(String userName,Model model){
        OrderInfoManager orderInfoManager = orderService.queryUserName(userName);
        System.out.println(orderInfoManager);
        List<OrderInfoManager> list = new ArrayList<OrderInfoManager>();
        list.add(orderInfoManager);
        if (orderInfoManager == null){
            list = orderService.selectOrder();
            model.addAttribute("error","未查到");

        }
        model.addAttribute("list",list);
        return "allOrder";
    }
}
